﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BLL;
using System.IO;
using Model;
using System.Threading;
using System.Diagnostics;
using Intorise.Framework;
using Config;
using Intorise.Common;
using WowAnswerHacker;

namespace AutoCheckGuildWar
{
    static class Program
    {
        public static bool ShutDown = false;//是否关机
        public static readonly string RegisterCode = Security.GetCode("WowAnswerHacker");
        public static Register RegisterForm;
        public static AutoCheckConfig CheckConfig = null;
        public static List<string> AnswerDict = new List<string>();

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {

                CheckConfig = new AutoCheckConfig();
                ConfigManager.BusinessConfig = CheckConfig;
                ConfigManager.Init();

                DictForm.LoadDict();

                if ((args.Length > 0 && args[0].ToLower() == "-c") || (ConfigManager.AppConfig.IsShowConsole))
                {
                    Intorise.API.Windows.AllocConsole();
                    Console.WriteLine("控制台已启动");
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                MainForm mainForm = new MainForm();
                RegisterForm = new Register();
                try
                {
                    Application.Run(mainForm);
                }
                catch
                {

                }

                List<AccountLoginView> list = new List<AccountLoginView>();
                foreach (AccountLoginView alv in mainForm.waitingList)
                {
                    if (alv.LoginStatus == AccountLoginStatus.ConnectionError || alv.LoginStatus == AccountLoginStatus.Logining || alv.LoginStatus == AccountLoginStatus.NeedCode || alv.LoginStatus == AccountLoginStatus.NoLogin)
                    {
                        list.Add(alv);
                    }
                }

                if (list.Count > 0)//有未完成的验证
                {
                    ConsoleBLL.WriteLineInfo("保存未完成的账户" + list.Count + "个.");
                    StreamWriter writer = File.CreateText(AppDomain.CurrentDomain.BaseDirectory + "laster.txt");
                    foreach (AccountLoginView account in list)
                    {
                        writer.WriteLine(account.Username + " " + account.Password);
                    }
                    writer.Flush();
                    writer.Close();
                }
                else
                {
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + "laster.txt");
                }

                if (ConfigManager.AppConfig.AutoShutdown && ShutDown)
                {
                    Process p = new Process();
                    ConsoleBLL.WriteLineWaring("开始自动关机，请输入命令：shutdown -a 以取消关机...");
                    p.StartInfo = new ProcessStartInfo("shutdown", "-s -f -t 120 -c 取消关机，\"请输入命令：shutdown -a\"");//设置2分钟超时关闭计算机
                    p.Start();
                }
            }
            finally
            {
                //关闭
                Intorise.API.Windows.FreeConsole();
            }

        }
    }
}